<?php
/**
 * 功能：mysql DB drive
 * 说明：mysql.class.php
 * 创建日期：2012-8-1
 * 更新日期：
 * 作者：Mirze
 * 补充说明： 
 * 
 */
abstract class adapter_mysql
{
	private $_db;
	private $_conn;
	private $_dsn; //非extends使用

	function __construct($dsn) {
		 
	}

	/**
	 * (功能描述) 获取DSN
	 *
	 * @param array $dsn 
	 * @return
	 */
	function getDsn($dsn)
	{
		if (! is_array($dsn)) {
            $dsn = db::getDSN($dsn);
        }		
        $this->_schema = $dsn['database'];

		$this->_dsn = $dsn; //非extends使用
	}

	/**
	 * (功能描述) 
	 *
	 * @param bool $pconnect 连接方式: true 长连接 false 短连接
	 * @return
	 */
	function connect($pconnect = false, $force_new = false)
    {
        if (is_resource($this->_conn)) { return; }


		 if ($pconnect) {
            $this->_conn = mysql_pconnect($host, $this->_dsn['login'], $this->_dsn['password'], $force_new);
        } else {
            $this->_conn = mysql_connect($host, $this->_dsn['login'], $this->_dsn['password'], $force_new);
        }

        if (! is_resource($this->_conn)) {
            throw new DbException('CONNECT DATABASE', mysql_error(), mysql_errno());
        }

        if (! empty($this->_dsn['database'])) {
            $this->execute('USE ' . $this->qid($this->_dsn['database']));
        }

        if (isset($this->_dsn['charset']) && $this->_dsn['charset'] != '') {
            $charset = $this->_dsn['charset'];
            $this->execute("SET NAMES '" . $charset . "'");
        }

		return $this->_conn;
	}

	/**
	 * (功能描述) 
	 *
	 * @param 
	 * @return
	 */
	function execute($sql, $inputarr = null)
    {
		if (is_array($inputarr)) {
			$sql = $this->_fakebind($sql, $inputarr);
		}

		if ($this->_log_enabled) {
			//QLog::log('[DB EXECUTE] ' . str_replace("\n", ' ', $sql), QLog::DEBUG);
		}

		if (! $this->_conn) {
			$this->connect();
		}
		$result = mysql_query($sql, $this->_conn);

		if (is_resource($result)) {
			//return new QDB_Result_Mysql($result, $this->_fetch_mode);
		} elseif ($result)  {
			$this->_last_err = null;
			$this->_last_err_code = null;
			return $result;
		} else  {
			$this->_last_err = mysql_error($this->_conn);
			$this->_last_err_code = mysql_errno($this->_conn);
			$this->_has_failed_query = true;

			if ($this->_last_err_code == 1062) {
				throw new DbException($sql, $this->_last_err, $this->_last_err_code);
			} else {
				throw new DbException($sql, $this->_last_err, $this->_last_err_code);
			}
		}
    }



}

?>